import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Node;


public class Authenticator {
	
	private Authenticator() {}	
	
	public static Session getMobileSession(String username, String password, String apikey, String secret) throws Exception {
		
		String authToken = createAuthToken(username, password);
		
		Map<String, String> parameterMap = new HashMap<String, String>();
		parameterMap.put("method", "auth.getMobileSession");
		parameterMap.put("username", username);
		parameterMap.put("authToken", authToken);
		parameterMap.put("api_key", apikey);
	
		// call method via POST-request
        Document xmlResponse = Method.call(parameterMap, secret);
        
        Session session = new Session();
        
        Node node = xmlResponse.getElementsByTagName("subscriber").item(0);        
		if ("1".equals(node.getTextContent())) {
			session.setSubscriber(true);
		}        		
    	
		node = xmlResponse.getElementsByTagName("key").item(0);    
		if ("key".equals(node.getNodeName())) {
    		session.setKey(node.getTextContent());
    	} 
		
		session.setSecret(secret);
		session.setApiKey(apikey);
		session.setAuthToken(authToken);
		session.setUsername(username);
		
		return session;
	}
	
	public static Station tune(Session session, StationType stationType, String stationName) throws Exception {
		
		Map<String, String> parameterMap = new HashMap<String, String>();
		parameterMap.put("method", "radio.tune");
		parameterMap.put("api_key", session.getApiKey());
		parameterMap.put("sk", session.getKey());
		
        switch (stationType) {
		case GLOBAL_TAG_RADIO:
			parameterMap.put("station", "lastfm://globaltags/" + stationName);			
			break;
		case SIMILAR_ARTISTS_RADIO: 
			parameterMap.put("station", "lastfm://artist/" + stationName + "/similarartists");			
			break;
		case TOP_FANS_RADIO:
			parameterMap.put("station", "lastfm://artist/" + stationName + "/fan");			
			break;
		}
        
        // call method via POST-request
        Document xmlResponse = Method.call(parameterMap, session.getSecret());
        String name = xmlResponse.getElementsByTagName("name").item(0).getTextContent();
        
        Station station = new Station();
        station.setName(name);
        
        return station;
	}

	private static String createAuthToken(String username, String password) throws NoSuchAlgorithmException {
		return CryptoTool.md5(username.toLowerCase() + CryptoTool.md5(password));
	}
	
	public static void main(String[] args) throws Exception {
		String API_KEY = "057b4272f51477b97e99d831725c3674";
		String API_SEC = "d9ebba247e3e15dd7b12c93478545b39";
		
		Session session = Authenticator.getMobileSession("Mick75", "317031", API_KEY, API_SEC);
		Station station = Authenticator.tune(session, StationType.GLOBAL_TAG_RADIO, "Trance");
		System.out.println(station.getName());
		for (Track track : station.getPlaylist(session)) {
			System.out.println(track.getLocation() + " " + " " + track.getDuration());
		}
	}
}
